<?php 
	function db(){
		$dbhost = 'localhost';
		$dbuser = 'symbiote_outword';
		$dbpass = 'th30l0gy23';
		$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
		//$dbname = 'symbiote_outword';
		//$dbname = 'symbiote_outwordbeta';
		$dbname = 'symbiote_outworddev';
		mysql_select_db($dbname);
	}

	//DATABASE QUERIES
	function dbSelect($table,$cond="",$orderby=""){
		$sql = "SELECT * FROM ".$table;
		if($cond!=""){
			$sql .= " WHERE ";
			$cnt = 0;
			foreach($cond as $eachcond){
				if($cnt==0){$sql .= $eachcond;}else{$sql .= " AND ".$eachcond;}
				$cnt++;
			}
		}
		if($orderby!=""){$sql .= ' '.$orderby;}
		$rows = mysql_query($sql);
		return $rows;
	}

	function dbUpdate($table,$keysvalues,$cond){
		$sql = "UPDATE ".$table." SET ";
		$kvcnt = 0;
		foreach($keysvalues as $keyvalue){
			if($kvcnt==0){$sql .= $keyvalue;}else{$sql .= ', '.$keyvalue;}
			$kvcnt++;
		}
		if($cond!==""){
			$sql .= " WHERE ";
			$cnt = 0;
			foreach($cond as $eachcond){
				if($cnt==0){$sql .= $eachcond;}else{$sql .= ' AND '.$eachcond;}
				$cnt++;
			}
		}
		$rows = mysql_query($sql);
		//echo $sql;
		return $rows;
	}

	function dbInsert($table,$keys,$values){
		$sql = "INSERT INTO ".$table." (";
		$kcnt = 0;
		foreach($keys as $key){
			if($kcnt==0){$sql .= $key;}else{$sql .= ','.$key;}
			$kcnt++;
		}		
		$sql .= ") VALUES (";
		$vcnt = 0;
		foreach($values as $value){
			if($vcnt==0){$sql .= $value;}else{$sql .= ','.$value;}
			$vcnt++;
		}
		$sql .= ")";
		$rows = mysql_query($sql);
		return $rows;
	}
	function dbDelete($table,$cond){
		$sql = "DELETE FROM ".$table;
		if($cond!=""){
			$sql .= " WHERE ";
			$cnt = 0;
			foreach($cond as $eachcond){
				if($cnt==0){$sql .= $eachcond;}else{$sql .= " AND ".$eachcond;}
				$cnt++;
			}
		}
		$rows = mysql_query($sql);
		return $rows;
	}
	
	//PAGE COMPRESSION
	function print_gzipped_page() {
		global $HTTP_ACCEPT_ENCODING;
		if( headers_sent() ){$encoding = false;
		}elseif( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ){$encoding = 'x-gzip';
		}elseif( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ){$encoding = 'gzip';
		}else{$encoding = false;}
		if( $encoding ){
			$contents = ob_get_contents();
			ob_end_clean();
			header('Content-Encoding: '.$encoding);
			print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
			$size = strlen($contents);
			$contents = gzcompress($contents, 9);
			$contents = substr($contents, 0, $size);
			print($contents);
			exit();
		}else{
			ob_end_flush();
			exit();
		}
	}
	function isEmailValid($email){
		if (preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/',$email)){
			return true;
		}else{
			return false;
		}
	}
	
	function replaceEmailShortCodes($body,$cond){
		$bodyparts = preg_match_all('/##([a-z0-9\._]+)##/i',$body,$matches);
		echo '<br />';
			foreach($matches[0] as $match=>$value){
				//echo '<br />'.$value;
				$strippedvalue = substr($value,2,-2);
				echo '<br />'.$strippedvalue;
				$tablefield = explode('.',$strippedvalue);
				$table = $tablefield[0];
				$field = $tablefield[1];
				/* CONTINUE EDITING HERE */
					foreach($cond as $thiscond){
						$matchedcondarray = explode('=',$thiscond);
						$matchedcond = $matchedcondarray[0];
						$matchedcondvalue = $matchedcondarray[1];
						if(stristr($matchedcond,$strippedvalue)){
							echo $matchedcondvalue;
						}
					}
		}
	}
	
	function sendEmail($siteid,$sendtoids,$template,$contact=NULL){
		$emailroot = 'http://dev.outword.org';
		$emailimages = $emailroot.'/images/email';
		$siteqry = mysql_fetch_array(dbSelect('sites',array("id=$siteid")));
		foreach($sendtoids as $thissendtoid){
			$userqry = mysql_fetch_array(dbSelect('users',array("id=$thissendtoid")));
			$templateqry = mysql_fetch_array(dbSelect('emails',array("template='$template'")));
			$excerpt = $templateqry['excerpt'];
			include('emails/_header.php');
			include('emails/'.$templateqry['file']);
			include('emails/_footer.php');
			$headers = "From: outword@outword.org\n";
			$headers .= 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			$headers .= 'X-Mailer: PHP/' . phpversion();
			$subject = '[outword] '.$subject;
			$thissendtoaddress = $userqry['email'];
			mail($thissendtoaddress,$subject,$body,$headers,"-finfo@outword.org");
			//echo '<h1>email sent to '.$thissendtoaddress.'</h1>';
		}
	}
	
	function dateFormat($thedate){
	//format: 2010-05-23
		$datearr = explode(' ',$thedate);
		$date = $datearr[0];
		$time = $datearr[1];
		$datesplit = explode('-',$date);
		$year = $datesplit[0];
		$month = $datesplit[1];
		$day = $datesplit[2];
		$redate = mktime(0,0,0,$month,$day,$year);
		return $redate;
	}
	
	function dateUnformat($thedate){
		// format: 05/23/2010
		$datearr = explode('/',$thedate);
		$year = (int)$datearr[2];
		//echo $year;
		$month = (int)$datearr[0];
		//echo $month;
		$day = (int)$datearr[1];
		//echo $day;
		$redate = mktime(0,0,0,$month,$day,$year);
		return $redate;
	}
	
	
	function nicetime($date){
		if(empty($date)) {
			return "";
		}
	   
		$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
		$lengths = array("60","60","24","7","4.35","12","10");
	   
		$now = time();
		$unix_date = strtotime($date);
	   
		// check validity of date
		if(empty($unix_date)) {   
			return "";
		}

		// is it future date or past date
		if($now > $unix_date) {   
			$difference     = $now - $unix_date;
			$tense         = "ago";
		}else{
			$difference     = $unix_date - $now;
			$tense         = "from now";
		}
		for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
			$difference /= $lengths[$j];
		}
		$difference = round($difference);
		if($difference != 1) {
			$periods[$j].= "s";
		}
		return "$difference $periods[$j] {$tense}";
	}
	/*
	$date = "2009-03-04 17:45";
	$result = nicetime($date); // 2 days ago
	*/

    function createAuthCode($email){
		$authcode = md5($email);
		$md5authcode =md5($authcode);
		$authcode = md5($md5authcode);
		return $authcode;
	}
	function goBack(){
		echo '<br /><a href="javascript: history.back(1);">go back</a>';
	}
	function goHome($thisroot){
		echo '<br /><a href="'.$thisroot.'">go home</a>';
	}
	function editLink($edititem){
		echo '<span style="margin-top: 0px;" class="fakelinkhighlight" onclick="editContent(\''.$edititem.'\');">edit</span>&nbsp;';
	}
	function editHeaderLink($edititem){
		echo '&nbsp;<span class="fakelinkhighlight" onclick="editContent(\''.$edititem.'\');">edit</span>&nbsp;';
	}
	function editDel($postid,$authorid,$type){
		$showlinks = 0;
		if($_SESSION['loggedinuserid'] && $_SESSION['loggedinuserid'] !=""){
			/* 
			echo '<br />postid: '.$postid;
			echo '<br />authorid: '.$authorid;
			echo '<br />type: '.$type;
			echo '<br />loggedin: '.$_SESSION['loggedinuserid'] ;
			echo '<br />isadmin: '.in_array(1,$_SESSION['access']);
			*/
			if($type=='feed'){
				if(($authorid == $_SESSION['loggedinuserid']) || (in_array(1,$_SESSION['access']))){
					$showlinks++;
				}
			}else if($type=='vision'){
				if(($authorid == $_SESSION['loggedinuserid']) || (in_array(1,$_SESSION['access']))){
					$showlinks++;
				}
			}else if($type=='comment'){
				if(($authorid == $_SESSION['loggedinuserid']) || (in_array(1,$_SESSION['access']))){
					$showlinks++;
				}
			}else if($type=='event'){
				if(($authorid == $_SESSION['loggedinuserid']) || (in_array(1,$_SESSION['access'])) || (in_array(2,$_SESSION['access']))){
					$showlinks++;
				}
			}else if($type=='childcare'){
				if(($authorid == $_SESSION['loggedinuserid']) || (in_array(1,$_SESSION['access'])) || (in_array(3,$_SESSION['access']))){
					$showlinks++;
				}
			}else if($type=='need'){
				if(($authorid == $_SESSION['loggedinuserid']) || (in_array(1,$_SESSION['access'])) || (in_array(4,$_SESSION['access']))){
					$showlinks++;
				}
			}else if($type=='prayer'){
				if(($authorid == $_SESSION['loggedinuserid']) || (in_array(1,$_SESSION['access'])) || (in_array(4,$_SESSION['access'])) || (in_array(5,$_SESSION['access']))){
					$showlinks++;
				}
			}else if($type=='people'){
				if(($authorid == $_SESSION['loggedinuserid']) || (in_array(1,$_SESSION['access'])) || (in_array(4,$_SESSION['access'])) || (in_array(6,$_SESSION['access']))){
					$showlinks++;
				}
			}else if($type=='donation' || $type=='stuff' || $type=='money'){
				if(($authorid == $_SESSION['loggedinuserid']) || (in_array(1,$_SESSION['access'])) || (in_array(4,$_SESSION['access'])) || (in_array(7,$_SESSION['access']))){
					$showlinks++;
				}
			}
			//echo '<br />show? '.$showlinks;
			if($showlinks > 0){
			echo '<br />';
				echo '<span id="edit" rel="'.$postid.'" class="fakelinkhighlight">edit</span>&nbsp;';
				echo '<span id="delete" rel="'.$postid.'" class="fakelinkhighlight">delete</span>';
				//echo '</div>';
			}
		}
	}
	function addPost($type){
		$showlink = 0;
		if($_SESSION['loggedinuserid'] && $_SESSION['loggedinuserid'] !=""){
		if($type=='feed'){
				if(in_array(1,$_SESSION['access'])){$showlink++;}
			}else if($type=='vision'){
				if(in_array(1,$_SESSION['access'])){$showlink++;}
			}else if($type=='comment'){
				if(in_array(1,$_SESSION['access'])){$showlink++;}
			}else if($type=='event'){
				if(in_array(1,$_SESSION['access']) || (in_array(2,$_SESSION['access']))){$showlink++;}
			}else if($type=='childcare'){
				if(in_array(1,$_SESSION['access']) || (in_array(3,$_SESSION['access']))){$showlink++;}
			}else if($type=='need'){
				if(in_array(1,$_SESSION['access']) || (in_array(4,$_SESSION['access']))){$showlink++;}
			}else if($type=='prayer'){
				if(in_array(1,$_SESSION['access']) || (in_array(4,$_SESSION['access'])) || (in_array(5,$_SESSION['access']))){$showlink++;}
			}else if($type=='people'){
				if(in_array(1,$_SESSION['access']) || (in_array(4,$_SESSION['access'])) || (in_array(6,$_SESSION['access']))){$showlink++;}
			}else if($type=='donation' || $type=='stuff' || $type=='money'){
				if(in_array(1,$_SESSION['access']) || (in_array(4,$_SESSION['access'])) || (in_array(7,$_SESSION['access']))){$showlink++;}
			}
			if($showlink > 0){echo '&nbsp;<span id="add" rel="'.$type.'" class="fakelinkhighlight">add</span>&nbsp;';}
		}
	}
	function addCommentLink(){
		if($_SESSION['loggedinuserid'] && $_SESSION['loggedinuserid'] > 0){
			echo '&nbsp;<span id="comment" class="fakelinkhighlight">add</span>&nbsp;';
		}
	}
	function yesNoDdl($id,$default){
		echo '<select id="'.$id.'" name="'.$id.'">';
		if($default==0){$no = ' SELECTED ';}else{$no = '';}
		if($default==1){$yes = ' SELECTED ';}else{$yes = '';}
		echo '<option value="-1">--select--</option>';
		echo '<option value="0"'.$no.'>no</option>';
		echo '<option value="1"'.$yes.'>yes</option>';
		echo '</select>';
	}
	function perDdl($id,$default){

	}
	function redirect($thisroot){
		echo '<script>window.location.href=\''.$thisroot.'\';</script>';
	}
	function helpLink($msg){
		echo '&nbsp;<span onmouseover="tooltip.show(\''.$msg.'\');" onmouseout="tooltip.hide();" style="text-decoration: none; cursor: help; font-size: 12pt; color: #00137F; font-weight: bold;">?</span>';
	}
	function roundme($type){
		if($type == 'page_top'){
			echo '<div style="height: 9px; padding: 0px 0px -5px 0px;"><img src="images/corners/white-top.png" /></div>';
		}
		if($type == 'page_bottom'){
			echo '<div style="height: 9px; padding: -5px 0px 0px 0px;"><img src="images/corners/white-bottom.png" /></div>';
		}
		if($type == 'sidebar_top'){
			echo '<div style="height: 9px;"><img src="images/corners/sidebar-top.png" /></div>';
		}
		if($type == 'sidebar_bottom'){
			echo '<div style="height: 9px;"><img src="images/corners/sidebar-bottom.png" /></div>';
		}
	}
?>